﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using RearLivestock.Common.Domains.hhx;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.Api.hhxApplication.Commands.UserTable;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Read.Api.hhxApplication.Commands.UserTable
{
    public class GetUnitsHandler : IRequestHandler<GetUnitsCommand, Result>
    {
        private readonly IBaseRepository<Units> unitsRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="unitsRepository"></param>
        public GetUnitsHandler(IBaseRepository<Units> unitsRepository)
        {
            this.unitsRepository = unitsRepository;
        }
        /// <summary>
        /// 获取单位列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(GetUnitsCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var res = await(await unitsRepository.GetAll()).ToListAsync();
                if (res != null)
                {
                    result.Data = res;
                    result.Code = (int)CommonErrorCode.Success;
                }
            }
            catch (Exception)
            {
                result.Msg = "证件类型列表获取异常";
            }
            return result;
        }
    }
}
